# -*- coding:utf-8 -*-
from data import models as data_models
import datetime


def save_base_info(data,main_ip):
    reportTime = datetime.datetime.now()
    count = data_models.server_main.objects.filter(main_ip=main_ip).count()
    if count == 0:
        cpu_obj = data_models.server_cpu.objects.create(
            model=data['cpu']['model'],
            frequency=data['cpu']['frequency'],
            thread=data['cpu']['thread'],
            core=data['cpu']['core']
        )
        platform_obj = data_models.server_platform.objects.create(
            vendor=data['platform']['vendor'],
            kernel=data['platform']['kernel'],
            virtual=data['platform']['virtual'],
            serial=data['platform']['serial'],
            arch=data['platform']['arch'],
            model=data['platform']['model'],
            os=data['platform']['os'],
        )
        main_obj = data_models.server_main.objects.create(
            reportTime=reportTime,
            main_ip=main_ip,
            hostname=data['main']['hostname'],
            memory_total_size=data['main']['memory_total_size'],
            memory_free_size=data['main']['memory_free_size'],
            disk_total_size=data['main']['disk_total_size'],
            cpu=cpu_obj,
            platform=platform_obj
        )
        for item in data['memory']:
            data_models.server_memory.objects.create(
                configSpeed=item['configSpeed'],
                type=item['type'],
                size=item['size'],
                speed=item['speed'],
                serial=item['serial'],
                server_main=main_obj
            )
        for item in data['partition']:
            data_models.server_partition.objects.create(
                file_system=item['file_system'],
                mount_point=item['mount_point'],
                total_size=item['total_size'],
                usage_size=item['usage_size'],
                server_main=main_obj
            )
        for item in data['network']:
            data_models.server_network.objects.create(
                interface=item['interface'],
                ip=item['ip'],
                mac=item['mac'],
                speed=item['speed'],
                statu=item['statu'],
                server_main=main_obj
            )
    else:
        data_models.server_main.objects.filter(main_ip=main_ip).update(
            reportTime=reportTime,
        )
        main_obj = data_models.server_main.objects.get(main_ip=main_ip)

        if 'network' in data.keys():
            mac_list = []
            for item in data['network']:
                if data_models.server_network.objects.filter(interface=item['interface'],
                                                             server_main__main_ip=main_ip).count() == 0:
                    data_models.server_network.objects.create(
                        interface=item['interface'],
                        ip=item['ip'],
                        mac=item['mac'],
                        speed=item['speed'],
                        statu=item['statu'],
                        server_main=main_obj
                    )
                else:
                    data_models.server_network.objects.filter(interface=item['interface'],
                                                              server_main__main_ip=main_ip).update(
                        interface=item['interface'],
                        ip=item['ip'],
                        mac=item['mac'],
                        speed=item['speed'],
                        statu=item['statu'],
                    )
                mac_list.append(item['mac'])
            for item in data_models.server_network.objects.filter(server_main__main_ip=main_ip):
                if not item.mac in mac_list:
                    data_models.server_network.objects.filter(interface=item['interface'],
                                                              server_main__main_ip=main_ip).delete()

        if 'partition' in data.keys():
            partition_list = []
            for item in data['partition']:
                if data_models.server_partition.objects.filter(server_main__main_ip=main_ip,
                                                               file_system=item['file_system']).count() == 0:
                    data_models.server_partition.objects.create(
                        file_system=item['file_system'],
                        mount_point=item['mount_point'],
                        total_size=item['total_size'],
                        usage_size=item['usage_size'],
                        server_main=main_obj
                    )
                else:
                    data_models.server_partition.objects.filter(server_main__main_ip=main_ip,
                                                                file_system=item['file_system']).update(
                        file_system=item['file_system'],
                        mount_point=item['mount_point'],
                        total_size=item['total_size'],
                        usage_size=item['usage_size'],
                    )
                partition_list.append(item['file_system'])

            for item in data_models.server_partition.objects.filter(server_main__main_ip=main_ip):
                if not item.file_system in partition_list:
                    data_models.server_partition.objects.filter(server_main__main_ip=main_ip,
                                                                file_system=item.file_system).delete()

        if 'memory' in data.keys():
            memory_list = []
            for item in data['memory']:
                if data_models.server_memory.objects.filter(server_main__main_ip=main_ip, serial=item['serial']) == 0:
                    data_models.server_memory.objects.create(
                        configSpeed=item['configSpeed'],
                        type=item['type'],
                        size=item['size'],
                        speed=item['speed'],
                        serial=item['serial'],
                        server_main=main_obj
                    )
                else:
                    data_models.server_memory.objects.filter(server_main__main_ip=main_ip,
                                                             serial=item['serial']).update(
                        configSpeed=item['configSpeed'],
                        type=item['type'],
                        size=item['size'],
                        speed=item['speed'],
                        serial=item['serial'],
                    )
                memory_list.append(item['serial'])

            for item in data_models.server_memory.objects.filter(server_main__main_ip=main_ip):
                if not item.serial in memory_list:
                    data_models.server_memory.objects.filter(server_main__main_ip=main_ip, serial=item.serial).delete()

        if 'platform' in data.keys():
            data_models.server_platform.objects.filter(server_main__main_ip=main_ip).update(
                vendor=data['platform']['vendor'],
                kernel=data['platform']['kernel'],
                virtual=data['platform']['virtual'],
                serial=data['platform']['serial'],
                arch=data['platform']['arch'],
                model=data['platform']['model'],
                os=data['platform']['os'],
            )

        if 'main' in data.keys():
            data_models.server_main.objects.filter(main_ip=main_ip).update(
                hostname=data['main']['hostname'],
                memory_total_size=data['main']['memory_total_size'],
                memory_free_size=data['main']['memory_free_size'],
                disk_total_size=data['main']['disk_total_size'],
            )

        if 'cpu' in data.keys():
            data_models.server_cpu.objects.filter(server_main__main_ip=main_ip).update(
                model=data['cpu']['model'],
                frequency=data['cpu']['frequency'],
                thread=data['cpu']['thread'],
                core=data['cpu']['core']
            )
